/* -----------------------------------------------------------------------------
 * PROJECT    : lwf_web
 * FILE       : GradeTag.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Apr 10, 2012  MyCodeTalks			       Created
 */

package net.lwf.web.tags;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class GradeTag extends BodyTagSupport {

	private static final long serialVersionUID = 1L;
	
	public int doAfterBody() throws JspException {
		try {
			BodyContent bodycontent = getBodyContent();
			String grade = bodycontent.getString();
			JspWriter out = bodycontent.getEnclosingWriter();
			if (grade != null) {
				out.print(this.getGradeName(grade));
			}
		} catch (IOException ioex) {
			throw new JspException("Error:" + ioex.getMessage());
		}
		return SKIP_BODY;
	}
	
	private String getGradeName(String grade) {
		if(grade != null) {
			int nGrade = Integer.parseInt(grade);
			switch(nGrade) {
				case  0: return "Kinder Garten";
				case -1: return "Pre K";
				case  1: return "1st";
				case  2: return "2nd";
				case  3: return "3rd";
				case  4: return "4th";
				case  5: return "5th";
				case  6: return "6th";
				default: return "Unknown";
			}
		}
		return "Unknown";
	}

}
